package com.gakj.truck.third.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gakj.truck.third.api.service.HuaTongService;
import com.gakj.truck.third.api.service.TestService;
import com.gakj.truck.third.api.service.TruckOumanService;
import com.gakj.truck.third.core.domain.Test;
import com.gakj.truck.third.core.domain.TruckOuman;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 该类的主要作用是测试一些功能
 * Created by gc on 2016/5/25.
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    TruckOumanService truckOumanService;


    @Autowired
    TestService testService;

    @Autowired
    HuaTongService huaTongService;


    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public JSONObject testOumanAndSnail(){
        JSONObject result = new JSONObject();
        //获取一个JSON字符串
        TruckOuman truckOuman = truckOumanService.getById(5L);

        String jsonString = JSON.toJSONString(truckOuman);

        try {
            truckOumanService.getTurckFromOuman(1);
            result.put("resMsg","成功!");
            result.put("resCode","1");
        }catch (Exception ex){
            ex.printStackTrace();
            result.put("resMsg","失败!");
            result.put("resCode","0");
        }

        return result;
    }

    @RequestMapping(value = "pk",method = RequestMethod.GET)
    public JSONObject testPrimaryKey(){

        JSONObject result = new JSONObject();

        Test test = new Test();
        test.setName("aa");
        test.setAge(18);
        Long newid = testService.save(test);
        System.out.println("保存生成的新的主键ID为:"+newid);

        System.out.println("新的ID:"+test.getId());

        result.put("pk",test.getId());
        result.put("resCode","1");
        result.put("resMsg","success");
        return result;
    }

    @RequestMapping(value = "/testHuatong",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject testHuaTongCreateTruck(
            @RequestParam(value = "id",defaultValue = "") Long id
    ) throws Exception {

        JSONObject result = new JSONObject();

        TruckOuman truckOuman = truckOumanService.getById(id);
        truckOuman.setId(0l);
        truckOuman.setTruckId(1009L);
        truckOuman.setRelationId(null);

//        Truck truck = huaTongService.transformToTruck(truckOuman, "save");

//        System.out.println(JSON.toJSONString(truck));
//        result.put("data",truck);

        JSONObject jsonObject = huaTongService.saveOrUpdate(truckOuman);

        System.out.println(jsonObject);
        return jsonObject;
    }

}
